package cn.handong.tool.controller;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Snake extends JFrame implements KeyListener, ActionListener {
    private Timer timer = new Timer(100, this);
    private int width = 400;
    private int height = 400;
    private int size = 10;
    private int snakeX[] = new int[width * height];
    private int snakeY[] = new int[width * height];
    private int foodX, foodY;
    private int LengthOfSnake = 2;
    private boolean left = false;
    private boolean right = true;
    private boolean up = false;
    private boolean down = false;

    public Snake() {
        setTitle("Snake");
        setSize(width, height);
        setLocationRelativeTo(null);
        setResizable(false);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setBackground(Color.BLACK);
        addKeyListener(this);
        startGame();
    }

    public void startGame() {
        left = false;
        right = true;
        up = false;
        down = false;
        LengthOfSnake = 2;
        for (int i = 0; i < LengthOfSnake; i++) {
            snakeX[i] = 50 - i * size;
            snakeY[i] = 50;
        }
        generateFood();
        timer.start();
    }

    public void generateFood() {
        foodX = (int) (Math.random() * (width - size));
        foodY = (int) (Math.random() * (height - size));
        foodX = foodX - foodX % size;
        foodY = foodY - foodY % size;
    }

    public void move() {
        for (int i = LengthOfSnake; i > 0; i--) {
            snakeX[i] = snakeX[i - 1];
            snakeY[i] = snakeY[i - 1];
        }
        if (left) {
            snakeX[0] -= size;
        }
        if (right) {
            snakeX[0] += size;
        }
        if (up) {
            snakeY[0] -= size;
        }
        if (down) {
            snakeY[0] += size;
        }
    }

    public void checkCollision() {
        for (int i = LengthOfSnake; i > 0; i--) {
            if (snakeX[0] == snakeX[i] && snakeY[0] == snakeY[i]) {
                timer.stop();
                JOptionPane.showMessageDialog(this, "Game Over!");
                startGame();
            }
        }
        if (snakeX[0] < 0) {
            snakeX[0] = width - size;
        }
        if (snakeX[0] >= width) {
            snakeX[0] = 0;
        }
        if (snakeY[0] < 0) {
            snakeY[0] = height - size;
        }
        if (snakeY[0] >= height) {
            snakeY[0] = 0;
        }
        if (snakeX[0] == foodX && snakeY[0] == foodY) {
            LengthOfSnake++;
            generateFood();
        }
    }

    public void paint(Graphics g) {
        super.paint(g);
        for (int i = 0; i < width; i += size) {
            g.drawLine(i, 0, i, height);
        }
        for (int i = 0; i < height; i += size) {
            g.drawLine(0, i, width, i);
        }
        for (int i = 0; i < LengthOfSnake; i++) {
            g.fillRect(snakeX[i], snakeY[i], size, size);
        }
        g.setColor(Color.RED);
        g.fillOval(foodX, foodY, size, size);
    }

    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT && !right) {
            left = true;
            up = false;
            down = false;
        }
        if (keyCode == KeyEvent.VK_RIGHT && !left) {
            right = true;
            up = false;
            down = false;
        }
        if (keyCode == KeyEvent.VK_UP && !down) {
            up = true;
            right = false;
            left = false;
        }
        if (keyCode == KeyEvent.VK_DOWN && !up) {
            down = true;
            right = false;
            left = false;
        }
    }

    public void actionPerformed(ActionEvent arg0) {
        if (arg0.getSource() == timer) {
            checkCollision();
            move();
            repaint();
        }
    }

    public void keyReleased(KeyEvent arg0) {
    }

    public void keyTyped(KeyEvent arg0) {
    }

    public static void main(String[] args){
    }
}